<script setup>
import { ref, onMounted } from "vue";
import {
  queryBuildingPageApi,
  addBuildingApi,
  queryBuildingByIdApi,
  updateBuildingApi,
  deleteBuildingApi,
} from "@/api/building";
import { ElMessage, ElMessageBox } from "element-plus";

// 查询表单
const searchForm = ref({ name: "", status: "" });

// 楼栋列表、分页
const buildingList = ref([]);
const total = ref(0);
const currentPage = ref(1);
const pageSize = ref(10);

const fetchBuildingList = async () => {
  const params = {
    name: searchForm.value.name,
    status: searchForm.value.status,
    page: currentPage.value,
    pageSize: pageSize.value,
  };
  const res = await queryBuildingPageApi(params);
  if (res.code) {
    buildingList.value = res.data.records;
    total.value = res.data.total;
  }
};

onMounted(() => {
  fetchBuildingList();
});

const handleSizeChange = () => fetchBuildingList();
const handleCurrentChange = () => fetchBuildingList();
const clear = () => {
  searchForm.value = { name: "", status: "" };
  fetchBuildingList();
};

// 弹窗表单
const dialogVisible = ref(false);
const dialogTitle = ref("新增楼栋");
const buildingForm = ref({
  name: "",
  code: "",
  status: 1,
  remark: "",
});
const buildingFormRef = ref();

const openAdd = () => {
  dialogTitle.value = "新增楼栋";
  dialogVisible.value = true;
  buildingForm.value = { name: "", code: "", status: 1, remark: "" };
  if (buildingFormRef.value) buildingFormRef.value.resetFields();
};

const openEdit = async (row) => {
  dialogTitle.value = "编辑楼栋";
  dialogVisible.value = true;
  const res = await queryBuildingByIdApi(row.id);
  if (res.code) {
    buildingForm.value = { ...res.data };
  }
};

const saveBuilding = () => {
  buildingFormRef.value.validate(async (valid) => {
    if (!valid) return ElMessage.error("表单校验不通过");
    let res;
    if (buildingForm.value.id) {
      res = await updateBuildingApi(buildingForm.value);
    } else {
      res = await addBuildingApi(buildingForm.value);
    }
    if (res.code) {
      ElMessage.success("保存成功");
      dialogVisible.value = false;
      fetchBuildingList();
    } else {
      ElMessage.error(res.msg);
    }
  });
};

const handleDelete = (row) => {
  ElMessageBox.confirm("确定要删除该楼栋吗？", "提示", { type: "warning" })
    .then(async () => {
      const res = await deleteBuildingApi(row.id);
      if (res.code) {
        ElMessage.success("删除成功");
        fetchBuildingList();
      } else {
        ElMessage.error(res.msg);
      }
    })
    .catch(() => {});
};

// 表单校验规则
const rules = {
  name: [{ required: true, message: "请输入楼栋名称", trigger: "blur" }],
  code: [{ required: true, message: "请输入编码", trigger: "blur" }],
  status: [{ required: true, message: "请选择状态", trigger: "change" }],
};
</script>

<template>
  <div>
    <!-- 查询表单 -->
    <el-form :inline="true" :model="searchForm" class="demo-form-inline" style="margin-bottom: 16px">
      <el-form-item label="楼栋名称">
        <el-input v-model="searchForm.name" placeholder="请输入楼栋名称" clearable />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
          <el-option label="启用" :value="1" />
          <el-option label="停用" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchBuildingList">查询</el-button>
        <el-button @click="clear">重置</el-button>
      </el-form-item>
      <el-form-item style="float:right; margin-left:auto;">
        <el-button type="primary" @click="openAdd">+ 新增楼栋</el-button>
      </el-form-item>
    </el-form>

    <!-- 楼栋表格 -->
    <el-table :data="buildingList" border stripe style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" />
      <el-table-column prop="name" label="楼栋名称" />
      <el-table-column prop="code" label="编码" />
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
            {{ scope.row.status === 1 ? '启用' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="openEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :total="total"
      :page-sizes="[5, 10, 20, 50]"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 16px; text-align: right;"
    />

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px" @close="buildingFormRef?.resetFields()">
      <el-form :model="buildingForm" :rules="rules" ref="buildingFormRef" label-width="100px">
        <el-form-item label="楼栋名称" prop="name">
          <el-input v-model="buildingForm.name" placeholder="请输入楼栋名称" />
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input v-model="buildingForm.code" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="buildingForm.status" placeholder="请选择状态">
            <el-option label="启用" :value="1" />
            <el-option label="停用" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="buildingForm.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveBuilding">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.demo-form-inline {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}
</style> 